package com.ruoyi.core.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.core.domain.*;
import com.ruoyi.core.domain.custombean.BatchActivationCodeSummaryListQueryResult;
import com.ruoyi.core.mapper.ActivationCodeDetailMapper;
import com.ruoyi.core.mapper.BatchActivationCodeSummaryMapper;
import com.ruoyi.core.mapper.ClientProductBatchActivationCodeSummaryRelationMapper;
import com.ruoyi.core.service.IBatchActivationCodeSummaryService;
import com.ruoyi.core.vo.BatchActivationCodeSummaryCreateOperationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * batch_coreService业务层处理
 *
 * @author ruoyi
 * @date 2020-09-28
 */
@Service
public class BatchActivationCodeSummaryServiceImpl implements IBatchActivationCodeSummaryService {
    @Autowired
    private BatchActivationCodeSummaryMapper batchActivationCodeSummaryMapper;

    @Autowired
    private ActivationCodeDetailMapper activationCodeDetailMapper;

    @Autowired
    private ClientProductBatchActivationCodeSummaryRelationMapper clientProductBatchActivationCodeSummaryRelationMapper;

    /**
     * 查询batch_core
     *
     * @param id batch_coreID
     * @return batch_core
     */
    @Override
    public BatchActivationCodeSummary selectBatchActivationCodeRecordById(Long id) {
        return batchActivationCodeSummaryMapper.selectBatchActivationCodeRecordById(id);
    }

    /**
     * 查询batch_core列表
     *
     * @param batchActivationCodeRecord batch_core
     * @return batch_core
     */
    @Override
    public List<BatchActivationCodeSummary> selectBatchActivationCodeRecordList(BatchActivationCodeSummary batchActivationCodeRecord) {
        return batchActivationCodeSummaryMapper.selectBatchActivationCodeRecordList(batchActivationCodeRecord);
    }

    /**
     * 新增batch_core
     *
     * @param batchActivationCodeSummaryCreateVo batch_core
     * @return 结果
     */
    @Override
    @Transactional
    public int createBatchActivationCodeSummary(BatchActivationCodeSummaryCreateOperationVo batchActivationCodeSummaryCreateVo) {

        //1、 批量激活码
        BatchActivationCodeSummary summary = new BatchActivationCodeSummary();
        BeanUtils.copyProperties(batchActivationCodeSummaryCreateVo, summary);
        Date nowDate = DateUtils.getNowDate();
        summary.setCreateTime(nowDate);
        summary.setRemark(batchActivationCodeSummaryCreateVo.getRemark());
        int result = batchActivationCodeSummaryMapper.insertBatchActivationCodeRecord(summary);

        // 2、激活码所能激活的客户端

        batchActivationCodeSummaryCreateVo.getProductIds().forEach(productId -> {
            ClientProductBatchActivationCodeSummaryRelation productBatchActivationCodeSummary = new ClientProductBatchActivationCodeSummaryRelation();

            productBatchActivationCodeSummary.setProductId(productId);
            productBatchActivationCodeSummary.setBatchId(summary.getId());
            productBatchActivationCodeSummary.setCreateTime(nowDate);
            clientProductBatchActivationCodeSummaryRelationMapper.insert(productBatchActivationCodeSummary);
        });

        //3、 激活码明细
        Long quantity = summary.getQuantity();
        //        Date date = DateUtils.incrementBaseOnBaseDate(nowDate, summary.getSegment().intValue(), timeUnit.getNumber());

        for (int i = 1; i <= quantity; i++) {
            ActivationCodeDetail detail = new ActivationCodeDetail();
            detail.setBatchId(summary.getId());
            detail.setStatus(ActivationCodeStatus.AVAILABLE);
            detail.setCode(getCode(IdUtils.simpleUUID()));
            detail.setCreateTime(nowDate);
            activationCodeDetailMapper.insertActivationCodeDetail(detail);
        }

        return result;
    }


    /**
     * 修改batch_core
     *
     * @param batchActivationCodeRecord batch_core
     * @return 结果
     */
    @Override
    public int updateBatchActivationCodeRecord(BatchActivationCodeSummary batchActivationCodeRecord) {
        return batchActivationCodeSummaryMapper.updateBatchActivationCodeRecord(batchActivationCodeRecord);
    }

    /**
     * 批量删除batch_core
     *
     * @param ids 需要删除的batch_coreID
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteBatchActivationCodeRecordByIds(Long[] ids) {

        clientProductBatchActivationCodeSummaryRelationMapper.deleteByBatchId(ids);
        activationCodeDetailMapper.deleteByBatchId(ids);
        batchActivationCodeSummaryMapper.deleteBatchActivationCodeRecordByIds(ids);
        return true;
    }

    /**
     * 删除batch_core信息
     *
     * @param id batch_coreID
     * @return 结果
     */
    @Override
    public int deleteBatchActivationCodeRecordById(Long id) {
        return batchActivationCodeSummaryMapper.deleteBatchActivationCodeRecordById(id);
    }

    @Override
    public List<BatchActivationCodeSummaryListQueryResult> findAll() {
        return batchActivationCodeSummaryMapper.findAll();
    }

    @Override
    public List<ClientProduct> findClientProductByBatchId(Long batchId) {
        return batchActivationCodeSummaryMapper.findClientProductByBatchId(batchId);
    }



    private String getCode(String uuid) {
        char[] chars = uuid.toCharArray();


        StringBuilder builder = new StringBuilder();

        for (int i = 1; i <= 8; i++) {

            int j = new Random().nextInt(chars.length);
            builder.append(chars[j]);

        }

        return builder.toString();

    }
}
